package org.wmq.taskschedule.algorithm;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

/**
 * 图像灰度化算法执行器
 * 实现AlgorithmExecutor接口，用于将彩色图像转换为灰度图像
 */
public class GrayscaleConversionExecutor implements AlgorithmExecutor {

    private int conversionType; // 转换类型
    private String description; // 算法描述

    /**
     * 默认构造函数，使用默认参数
     */
    public GrayscaleConversionExecutor() {
        this.conversionType = Imgproc.COLOR_BGR2GRAY;
        this.description = "图像灰度化算法 - 将彩色图像转换为灰度图像";
    }

    /**
     * 构造函数，允许自定义参数
     * 
     * @param conversionType 转换类型
     */
    public GrayscaleConversionExecutor(int conversionType) {
        this.conversionType = conversionType;
        this.description = "图像灰度化算法 - 将彩色图像转换为灰度图像";
    }

    /**
     * 构造函数，允许自定义所有参数
     * 
     * @param conversionType 转换类型
     * @param description 算法描述
     */
    public GrayscaleConversionExecutor(int conversionType, String description) {
        this.conversionType = conversionType;
        this.description = description;
    }

    @Override
    public Mat execute(Mat input) {
        if (input == null || input.empty()) {
            throw new IllegalArgumentException("输入图像不能为空");
        }

        // 创建输出图像
        Mat output = new Mat();

        // 如果输入已经是灰度图像，则直接复制
        if (input.channels() == 1) {
            input.copyTo(output);
        } else {
            // 应用灰度化转换
            Imgproc.cvtColor(input, output, conversionType);
        }

        return output;
    }

    // Getters and setters
    public int getConversionType() {
        return conversionType;
    }

    public void setConversionType(int conversionType) {
        this.conversionType = conversionType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}